﻿@addTagHelper *, WebVella.Erp.Plugins.Core
@addTagHelper *, WebVella.Erp.Web
@addTagHelper *, WebVella.TagHelpers
@using WebVella.Erp.Web.Utils;
@using WebVella.Erp.Web.Components;
@using WebVella.Erp.Web;
@using WebVella.Erp.Web.Models;
@{
	var options = (PcPageHeader.PcPageHeaderOptions)ViewBag.Options;
	var title = (string)ViewBag.ProccessedTitle;
	var subTitle = (string)ViewBag.ProccessedSubTitle;
	var areaLabel = (string)ViewBag.ProccessedAreaLabel;
	var areaSubLabel = (string)ViewBag.ProccessedAreaSubLabel;
	var description = (string)ViewBag.ProccessedDescription;
	var color = (string)ViewBag.ProccessedColor;
	var iconColor = (string)ViewBag.ProccessedIconColor;
	var iconClass = (string)ViewBag.ProccessedIconClass;
	var returnUrl = (string)ViewBag.ProccessedReturnUrl;

	var node = (PageBodyNode)ViewBag.Node;
	var erpRequest = (ErpRequestContext)ViewBag.RequestContext;
	var componentContext = (PageComponentContext)ViewBag.ComponentContext;
	var actionsNodes = node.Nodes.FindAll(x => x.ContainerId == "actions");
	var actionsAuxNodes = node.Nodes.FindAll(x => x.ContainerId == "actions-aux");
	var toolbarNodes = node.Nodes.FindAll(x => x.ContainerId == "toolbar");
	var pageSwitchItems = (List<PageSwitchItem>)ViewBag.PageSwitchItems;
}

<wv-page-header color="@color" icon-color="@iconColor" area-label="@areaLabel" area-sublabel="@areaSubLabel" title="@title"
	subtitle="@subTitle" description="@description" icon-class="@iconClass" return-url="@returnUrl" 
	page-switch-items="@pageSwitchItems" is-visible="(bool)ViewBag.IsVisible" fix-on-scroll="@options.FixOnScroll">
	@if(actionsNodes.Count > 0){
	<wv-page-header-actions>
		@foreach (var childNode in actionsNodes)
		{
			var nodeComponentName = "";
			if (childNode != null)
			{
				var nameArray = childNode.ComponentName.Split('.');
				nodeComponentName = nameArray[nameArray.Length - 1];
			}
			if (!String.IsNullOrWhiteSpace(nodeComponentName))
			{
				var childOptions = PageUtils.ConvertStringToJObject(childNode.Options.ToString());
				var pcContext = new PageComponentContext(childNode, componentContext.DataModel, ComponentMode.Display, childOptions, componentContext.Items);
				@await Component.InvokeAsync(nodeComponentName, new { context = pcContext })
			}
		}
	</wv-page-header-actions>
	}

	@if(actionsAuxNodes.Count > 0){
	<wv-page-header-actions-aux>
		@foreach (var childNode in actionsAuxNodes)
		{
			var nodeComponentName = "";
			if (childNode != null)
			{
				var nameArray = childNode.ComponentName.Split('.');
				nodeComponentName = nameArray[nameArray.Length - 1];
			}
			if (!String.IsNullOrWhiteSpace(nodeComponentName))
			{
				var childOptions = PageUtils.ConvertStringToJObject(childNode.Options.ToString());
				var pcContext = new PageComponentContext(childNode, componentContext.DataModel, ComponentMode.Display, childOptions, componentContext.Items);
				@await Component.InvokeAsync(nodeComponentName, new { context = pcContext })
			}
		}
	</wv-page-header-actions-aux>
	}

	@if (toolbarNodes.Count > 0)
	{
	<wv-page-header-toolbar>
		@foreach (var childNode in toolbarNodes)
		{
			var nodeComponentName = "";
			if (childNode != null)
			{
				var nameArray = childNode.ComponentName.Split('.');
				nodeComponentName = nameArray[nameArray.Length - 1];
			}
			if (!String.IsNullOrWhiteSpace(nodeComponentName))
			{
				var childOptions = PageUtils.ConvertStringToJObject(childNode.Options.ToString());
				var pcContext = new PageComponentContext(childNode, componentContext.DataModel, ComponentMode.Display, childOptions, componentContext.Items);
				@await Component.InvokeAsync(nodeComponentName, new { context = pcContext })
			}
		}
	</wv-page-header-toolbar>
	}
</wv-page-header>